package com.ting.test.algorithms.大厂刷题班;

import java.util.HashMap;
import java.util.HashSet;

/**
 * 有一个数组 给定一个目标数target
 * 找到并返回任意两个能够组成target的数的元素位置
 */
public class 两数之和 {
    public static void main(String[] args) {
        int[] a = new int[]{23, 12, 4, 21, 4, 43};

        int[] esuilt = process(a, 10);
    }

    private static int[] process(int[] a, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();

        for (int i = 0; i < a.length; i++) {
            int t = target - a[i];
            if (map.containsKey(t)) {
                return new int[]{map.get(t), i};
            }
            map.put(a[i], i);
        }
        return new int[]{-1, -1};
    }
}
